package cn.sciento.farm.devicemanager.quartz.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevTimer;
import cn.sciento.farm.devicemanager.quartz.SwitchJobContent;
import cn.sciento.farm.devicemanager.quartz.service.QuartzService;
import cn.sciento.farm.devicemanager.quartz.service.QuartzTimerService;
import org.apache.commons.lang3.StringUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author wumu
 * @version V1.0
 * @date 9/6/18
 * @time 4:26 PM
 */
@Service
public class QuartzTimerServiceImpl implements QuartzTimerService {

    private static final String PREFIX = "collection";

    private final static String REGULAR_CRON = "%d %d %d ? * %s *";

    @Autowired
    QuartzService quartzService;


    @Transactional()
    @Override
    public int insert(DevTimer devTimer) throws SchedulerException {
        String loop = devTimer.getCirculation();
        int time = devTimer.getStartTime();
        if (StringUtils.isEmpty(loop)) {
            return 0;
        }
        String cron = generateCron(time, loop);
        if (StringUtils.isEmpty(cron)) {
            return 0;
        }

        SwitchJobContent switchJobContent = new SwitchJobContent();
        switchJobContent.setControllerId(devTimer.getControllerId());
        switchJobContent.setStatus(devTimer.getStatus());
        switchJobContent.setTimerId(devTimer.getId());
        boolean en = quartzService.addJob(PREFIX + devTimer.getId(), PREFIX, cron, switchJobContent);
        if(en){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int deleteById(Long id) throws SchedulerException {
        boolean en = quartzService.removeJob(PREFIX + id, PREFIX);
        if (en){
            return 1;
        }else {
            return 0;
        }
    }

    @Transactional()
    @Override
    public int update(DevTimer devTimer) throws SchedulerException {
        String loop = devTimer.getCirculation();
        int time = devTimer.getStartTime();
        if (StringUtils.isEmpty(loop)) {
            return 0;
        }
        String cron = generateCron(time, loop);
        if (StringUtils.isEmpty(cron)) {
            return 0;
        }
        SwitchJobContent switchJobContent = new SwitchJobContent();
        switchJobContent.setControllerId(devTimer.getControllerId());
        switchJobContent.setStatus(devTimer.getStatus());
        switchJobContent.setTimerId(devTimer.getId());
        boolean en =quartzService.rescheduleJob(PREFIX + devTimer.getId(), PREFIX, cron, switchJobContent);
        if(en){
            return 1;
        }else {
            return 0;
        }
    }

    private String generateCron(int time, String loop) {
        if (time < 0 || time > 86400 || loop == null) {
            return null;
        }

        int sec = time % 60;
        int minute = time / 60 % 60;
        int hour = time / 60 / 60 % 60;

        String cron = String.format(REGULAR_CRON, sec, minute, hour, loop);
        return cron;
    }
}
